//
//  CYVideoListTableViewCell.m
//  cyydt
//
//  Created by chu on 2019/1/17.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYVideoListTableViewCell.h"
#import "CYHomeVideoDetailsViewController.h"

@implementation CYVideoListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.playTimeLabel.layer.cornerRadius = 23 / 2;
    self.playTimeLabel.layer.masksToBounds = YES;
    self.playTimeLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    self.userHeadImageView.layer.cornerRadius = self.userHeadImageView.frame.size.height / 2;
    self.userHeadImageView.layer.masksToBounds = YES;
}

- (void)setModel:(CYProjectModel *)model{
    _model = model;
    self.videoTitleLabel.text = model.title;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_url] placeholderImage:[UIImage imageNamed:@"videoPlaceHolde"]];
    [self.userHeadImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.userHeadImageView.frame.size]];
    self.nicknameLabel.text = model.nickname;
    [self.zanBtn setTitle:[NSString stringWithFormat:@"  %@",model.like_number] forState:UIControlStateNormal];
    [self.pinglunBtn setTitle:[NSString stringWithFormat:@"  %@",model.comment_number] forState:UIControlStateNormal];
    if ([NSString stringIsNull:model.play_time_format]) {
        self.playTimeLabel.hidden = YES;
    }else{
        self.playTimeLabel.text = model.play_time_format;
        self.playTimeLabel.hidden = NO;
    }
    
    if ([model.is_follow integerValue] == 1) {
        [self.guanzhuBtn setTitle:@"已关注" forState:UIControlStateNormal];
        [self.guanzhuBtn setTitleColor:RGBOF(0x999999) forState:UIControlStateNormal];
    }else{
        [self.guanzhuBtn setTitle:@"关注" forState:UIControlStateNormal];
        [self.guanzhuBtn setTitleColor:RGBOF(0xFE7676) forState:UIControlStateNormal];
    }
    
    if ([model.is_like integerValue] == 1) {
        self.zanBtn.selected = YES;
    }else{
        self.zanBtn.selected = NO;
    }
    
    if ([model.is_collect integerValue] == 1) {
        self.shoucangBtn.selected = YES;
    }else{
        self.shoucangBtn.selected = NO;
    }
}

- (IBAction)guanzhuAction:(UIButton *)sender {
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    
//    if (self.guanzhuBlock) {
//        self.guanzhuBlock();
//    }
    if ([self.model.is_follow integerValue] == 1) {
        [self cancleguanzhuWithModel:self.model];
    }else{
        [self guanzhuWithModel:self.model];
    }
}

- (IBAction)zanAction:(UIButton *)sender {
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
//    if (self.zanBlock) {
//        self.zanBlock();
//    }
    if ([self.model.is_like integerValue] == 1) {
        [self cancleZanWithModel:self.model];
    }else{
        [self zanWithModel:self.model];
    }

}

- (IBAction)pinglunAction:(UIButton *)sender {
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    CYHomeVideoDetailsViewController *video = [[CYHomeVideoDetailsViewController alloc] init];
    video.videoId = [self.model.ID stringValue];
    [[AppDelegate sharedAppDelegate] pushViewController:video];

}

- (IBAction)shoucangAction:(UIButton *)sender {
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
//    if (self.collectionBlock) {
//        self.collectionBlock();
//    }
    if ([self.model.is_collect integerValue] == 1) {
        [self cancleCollectionWithModel:self.model];
    }else{
        [self collectionWithModel:self.model];
    }

}


#pragma mark - 收藏取消收藏
- (void)cancleCollectionWithModel:(CYProjectModel *)model{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@/%@",User_HOST, @"collection",model.ID];
    NSDictionary *param = @{@"type":@"28", @"articleId":model.ID};
    [[XBRequest sharedInstance] deleteDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"取消收藏本视频 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_collect = [NSNumber numberWithInteger:2];
                NSInteger tag = [self.model.collect_number integerValue];
                self.model.collect_number = [NSString stringWithFormat:@"%ld",(tag - 1)];
                [self setModel:self.model];
                
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}

- (void)collectionWithModel:(CYProjectModel *)model{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"collection/addCollect"];
    NSDictionary *param = @{@"type":@28, @"articleId":model.ID};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"收藏本咨询 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_collect = [NSNumber numberWithInteger:1];
                NSInteger tag = [self.model.collect_number integerValue];
                self.model.collect_number = [NSString stringWithFormat:@"%ld",(tag + 1)];
                [self setModel:self.model];
                
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}

#pragma mark - 赞h和取消点赞
- (void)cancleZanWithModel:(CYProjectModel *)model{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"cancleLike"];
    NSDictionary *param = @{@"type":@1, @"articleId":model.ID, @"origin":@"28", @"toLikerId":model.user_id};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"取消点赞 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_like = [NSNumber numberWithInteger:2];
                NSInteger tag = [self.model.like_number integerValue];
                self.model.like_number = [NSString stringWithFormat:@"%ld",(tag - 1)];
                [self setModel:self.model];
                
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}

- (void)zanWithModel:(CYProjectModel *)model{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"addLike"];
    NSDictionary *param = @{@"type":@"1", @"likeId":model.ID, @"origin":@"28", @"toLikerId":model.user_id};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"赞视频 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_like = [NSNumber numberWithInteger:1];
                NSInteger tag = [self.model.like_number integerValue];
                self.model.like_number = [NSString stringWithFormat:@"%ld",(tag + 1)];
                [self setModel:self.model];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}


#pragma mark - 关注
- (void)guanzhuWithModel:(CYProjectModel *)model{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"attention"];
    NSDictionary *param = @{@"toUserId":model.user_id};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"添加关注 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_follow = [NSNumber numberWithInteger:1];
                [self setModel:self.model];

            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

- (void)cancleguanzhuWithModel:(CYProjectModel *)model{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@/%@",User_HOST, @"attention", model.user_id];
    NSDictionary *param = @{@"userId":model.user_id};
    [[XBRequest sharedInstance] deleteDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"取消关注 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            [[UIApplication sharedApplication].keyWindow makeToast:ResErrorMessage duration:1.5 position:CSToastPositionCenter];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                self.model.is_follow = [NSNumber numberWithInteger:2];
                [self setModel:self.model];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}
@end
